import { logger, createServer, Command, ModeOptions, LogLevelOptions } from 'f2e-server3'
import { createMiddleRBAC, createServiceMongo } from 'f2e-middle-rbac'
import { db } from './db'
import app from './app'

const command = new Command('filter_web')

command
    .option('-p, --port <port>', '端口号', 7707)
    .option('-m, --mode <mode>', '运行模式', 'prod', ModeOptions)
    .option('-l, --level <level>', '日志级别', 'DEBUG', LogLevelOptions)
    .action(async (options) => {
        const { port, mode, level } = options
        logger.setLevel(level)
        const service = createServiceMongo({ db })
        createServer({
            mode,
            port,
            buildFilter: pathname => /^(index|static|$)/.test(pathname),
            try_files: 'index.html',
            middlewares: [
                createMiddleRBAC(service, { logout_url: '/logout' }),
                app,
            ],
            postcss: {
                entryPoints: {
                    in: 'client/main.css',
                    out:'static/main.css',
                },
            },
            auth: {
                store: service.userService,
            },
            dest: 'filter_web',
        })
    })

command.parse(process.argv)